package api.cherry.controller;

import api.cherry.feign.CrApiLoginService;
import cherry.data.util.Result;
import cherry.data.view.request.CherryRequest;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;


/**
 * @author 谢宇翔
 * @version 1.0.0
 * @ClassName CherryLoginController.java
 * @Description 登录试图层
 * @createTime 2022年01月06日 23:18:00
 */
@Api(tags = "登录接口")
@RestController
@RequestMapping("/api/v1/cherry")
public class CherryLoginController {

    @Resource
    private CrApiLoginService loginService;

    /**
     * @return
     * @ClassName CherryLoginController.java
     * @author 谢宇翔
     * @version 1.0.0
     * @Description 登录接口
     * @createTime 2022年01月11日 21:29:00
     */
    @ApiOperation("登录")
    @PostMapping("/login")
    public Result<?> create(@RequestBody CherryRequest request) {
        if (StrUtil.isEmpty(request.getVerificationCode()) || StrUtil.isEmpty(request.getCode())) {
            return new Result<>(901, "请填写验证码");
        }
        if (!request.getVerificationCode().equals(request.getCode())) {
            return new Result<>(901, "验证码填写失败");
        }
        if (request.getUserAccount() == null || request.getPassword() == null) {
            return new Result<>(901, "账号密码");
        }
        return loginService.logIns(request);
    }


    /**
     * @ClassName CherryLoginController.java
     * @author 谢宇翔
     * @version 1.0.0
     * @Description 生成验证码
     * @createTime 2022年02月12日 17:33:00
     */
    @ApiOperation("生成验证码")
    @PostMapping("/account")
    public Result<String> register() throws IOException {
//        AccountUtil.ValidateCode vCode = new AccountUtil.ValidateCode(160, 40, 4, 150);
//            String path = "D:/" + new Date().getTime() + ".png";
//            System.out.println(vCode.getCode() + " >" + path);
//            vCode.write(path);
//        String code = vCode.getCode();
//        System.out.println(code);


        return new Result<>(600, "操作成功", getRandomFourNum());
    }


    public static String getRandomFourNum() {
        String[] beforeShuffle = new String[]{"2", "3", "4", "5", "6", "7",
                "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
                "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
                "W", "X", "Y", "Z"};
        List list = Arrays.asList(beforeShuffle);
        Collections.shuffle(list);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i));
        }
        String afterShuffle = sb.toString();
        String result = afterShuffle.substring(5, 9);
        return result;
    }

    /**
     * @ClassName CherryController.java
     * @author 谢宇翔
     * @version 1.0.0
     * @Description 注册接口
     * @createTime 2022年01月11日 21:29:00
     */
    @ApiOperation("注册接口")
    @PostMapping("/register")
    public Result<?> register(@RequestBody CherryRequest request) {
        if (ObjectUtils.isEmpty(request.getPassword()) || ObjectUtils.isEmpty(request.getUserAccount())) {
            return new Result<>(901, "必填信息不能为空");
        }
        if (!request.getVerificationCode().equals(request.getCode())) {
            return new Result<>(901, "验证码填写失败");
        }
        if (request.getUserAccount() == null || request.getPassword() == null) {
            return new Result<>(901, "账号密码");
        }
        if (!request.getPassword().equals(request.getConfirmPassword())) {
            return new Result<>(901, "确认密码和密码不相等");
        }
        return loginService.register(request);
    }

}
